/// 用户数据模型
class User {
  final int id;
  final String name;
  final String email;
  final String? avatar;
  final bool isActive;

  const User({
    required this.id,
    required this.name,
    required this.email,
    this.avatar,
    this.isActive = false,
  });

  /// 从 JSON 创建 User 对象
  factory User.fromJson(Map<String, dynamic> json) {
    return User(
      id: json['id'] as int,
      name: json['name'] as String,
      email: json['email'] as String,
      avatar: json['avatar'] as String?,
      isActive: json['isActive'] as bool? ?? false,
    );
  }

  /// 转换为 JSON
  Map<String, dynamic> toJson() {
    return {
      'id': id,
      'name': name,
      'email': email,
      'avatar': avatar,
      'isActive': isActive,
    };
  }

  /// 复制对象
  User copyWith({
    int? id,
    String? name,
    String? email,
    String? avatar,
    bool? isActive,
  }) {
    return User(
      id: id ?? this.id,
      name: name ?? this.name,
      email: email ?? this.email,
      avatar: avatar ?? this.avatar,
      isActive: isActive ?? this.isActive,
    );
  }

  @override
  bool operator ==(Object other) {
    if (identical(this, other)) return true;
    return other is User &&
        other.id == id &&
        other.name == name &&
        other.email == email &&
        other.avatar == avatar &&
        other.isActive == isActive;
  }

  @override
  int get hashCode {
    return Object.hash(id, name, email, avatar, isActive);
  }

  @override
  String toString() {
    return 'User(id: $id, name: $name, email: $email, avatar: $avatar, isActive: $isActive)';
  }
}

/// 文章数据模型
class Post {
  final int id;
  final String title;
  final String content;
  final int userId;
  final String? imageUrl;
  final int likes;
  final DateTime createdAt;

  const Post({
    required this.id,
    required this.title,
    required this.content,
    required this.userId,
    this.imageUrl,
    this.likes = 0,
    required this.createdAt,
  });

  /// 从 JSON 创建 Post 对象
  factory Post.fromJson(Map<String, dynamic> json) {
    return Post(
      id: json['id'] as int,
      title: json['title'] as String,
      content: json['content'] as String,
      userId: json['userId'] as int,
      imageUrl: json['imageUrl'] as String?,
      likes: json['likes'] as int? ?? 0,
      createdAt: DateTime.parse(json['createdAt'] as String),
    );
  }

  /// 转换为 JSON
  Map<String, dynamic> toJson() {
    return {
      'id': id,
      'title': title,
      'content': content,
      'userId': userId,
      'imageUrl': imageUrl,
      'likes': likes,
      'createdAt': createdAt.toIso8601String(),
    };
  }

  /// 复制对象
  Post copyWith({
    int? id,
    String? title,
    String? content,
    int? userId,
    String? imageUrl,
    int? likes,
    DateTime? createdAt,
  }) {
    return Post(
      id: id ?? this.id,
      title: title ?? this.title,
      content: content ?? this.content,
      userId: userId ?? this.userId,
      imageUrl: imageUrl ?? this.imageUrl,
      likes: likes ?? this.likes,
      createdAt: createdAt ?? this.createdAt,
    );
  }

  @override
  bool operator ==(Object other) {
    if (identical(this, other)) return true;
    return other is Post &&
        other.id == id &&
        other.title == title &&
        other.content == content &&
        other.userId == userId &&
        other.imageUrl == imageUrl &&
        other.likes == likes &&
        other.createdAt == createdAt;
  }

  @override
  int get hashCode {
    return Object.hash(id, title, content, userId, imageUrl, likes, createdAt);
  }

  @override
  String toString() {
    return 'Post(id: $id, title: $title, content: $content, userId: $userId, imageUrl: $imageUrl, likes: $likes, createdAt: $createdAt)';
  }
}